﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DALScolarite;

namespace Scolarite 
{
    public partial class FicheFormation : Form
    {

        public modeload loadingmod;
        public string idEltDiplome ="";

        public FicheFormation()
        {
            InitializeComponent();
        }

        private void idTypeEltDiplomeLabel_Click(object sender, EventArgs e)
        {

        }

        private void FicheFormation_Load(object sender, EventArgs e)
        {
            scolariteeltdiplomeBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_eltdiplome.ToList();
            scolaritetypeeltdiplomeBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_typeeltdiplome.ToList();
            if (this.loadingmod == modeload.AJOUT)
            {
                scolarite_eltdiplomeBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_eltdiplome.ToList();
                scolarite_eltdiplomeBindingSource.AddNew();
            }
            else if (this.loadingmod == modeload.INFO && this.idEltDiplome != string.Empty)
            {
                scolarite_eltdiplomeBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_eltdiplome.Where(et => et.idEltDiplome == idEltDiplome).ToList();

            }
        }

        private void btsave_Click(object sender, EventArgs e)
        {
            try
            {
                scolarite_eltdiplome adddiplome = new scolarite_eltdiplome
                    {
                        idEltDiplome = idEltDiplomeTextBox.Text,
                        idTypeEltDiplome = idTypeEltDiplomeComboBox.SelectedValue.ToString(),
                        labelfr = labelfrTextBox.Text,
                        labelan = labelanTextBox.Text,
                        labelar = labelarTextBox.Text,
                        NomComplet=nomCompletTextBox.Text                      
                    };

                if (this.idPereEltDiplomeComboBox.SelectedValue != null )
                {
                    adddiplome.idPereEltDiplome = idPereEltDiplomeComboBox.SelectedValue.ToString();
                }

                if (this.loadingmod == modeload.AJOUT)
                {
                    GlobalParams.MonDbcontext.scolarite_eltdiplome.Add(adddiplome);
                    GlobalParams.MonDbcontext.SaveChanges();
                    MessageBox.Show("Nouveau enregistrement a été inseret");
                }
                else
                {

                      uvtmoyEntities monentity = new uvtmoyEntities();
                      monentity.scolarite_eltdiplome.Attach(adddiplome);
                      monentity.Entry(adddiplome).State =  EntityState.Modified;

                      monentity.SaveChanges();  
                     
                   // GlobalParams.MonDbcontext.SaveChanges();
                    MessageBox.Show("enregistrement a été Modifier");
                }


                this.Close();
            }
            catch (DBConcurrencyException mes)
            {
                MessageBox.Show(mes.Message);
            }

        }

        private void labelfrTextBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
